package com.example.arithmeticleetcode.leetcode;

import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-05-16 17:54
 **/
public class Demo19 {

    public static void main(String[] args) {
        int[] arr1 = {40, 10, 20, 30};
        int[] arr2 = {100, 100, 100};
        int[] arr3 = {37, 12, 28, 9, 100, 56, 80, 5, 12};
        arrayRankTransform(arr1);
        arrayRankTransform(arr2);
        arrayRankTransform(arr3);
    }

    public static int[] arrayRankTransform(int[] arr) {
        List<Integer> list = new ArrayList<>();
        for (int i : arr) {
            list.add(i);
        }
        Arrays.sort(arr);
        Map<Integer, Integer> map = new HashMap<>();
        int count = 0;
        for (int i : arr) {
            if (!map.containsKey(i)) {
                map.put(i, ++count);
            }
        }
        int[] resultArr = new int[arr.length];
        for (int i = 0, len = arr.length; i < len; i++) {
            resultArr[i] = map.get(list.get(i));
        }
        return resultArr;
    }

}
